package com.css.order.controller;

import com.css.order.VO.ResultVO;
import com.css.order.client.ProductClient;
import com.css.order.dataobject.ProductInfo;
import com.css.order.dto.CartDTO;
import com.css.order.util.ResultVOUtil;
import com.sun.javafx.binding.StringFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * -
 * </p>
 *
 * @author css
 * @since 2019/12/27
 */
@RestController
public class ClientController {
    //
//    @Autowired
//    private LoadBalancerClient balancerClient;
//    @Autowired
//    private RestTemplate restTemplate;
    @Autowired
    private ProductClient productClient;

    @GetMapping("/getProductMsg")
    public String getProductMsg() {
//第一种方式
//        RestTemplate restTemplate = new RestTemplate();
//        String forObject = restTemplate.getForObject("http://localhost:10001/msg", String.class);

//        第二种方式
//        ServiceInstance serviceInstance = balancerClient.choose("PRODUCT");
//        String format = String.format("http://%s:%s", serviceInstance.getHost(), serviceInstance.getPort()) + "msg";
//        RestTemplate restTemplate = new RestTemplate();
//        String forObject = restTemplate.getForObject(format, String.class);

//        第三种方式
//        String forObject = restTemplate.getForObject("http://PRODUCT/msg", String.class);
        String msg = productClient.msg();
        return msg;
    }

    @GetMapping("/listForOrder")
    public ResultVO listForOrder() {
//第一种方式
//        RestTemplate restTemplate = new RestTemplate();
//        String forObject = restTemplate.getForObject("http://localhost:10001/msg", String.class);

//        第二种方式
//        ServiceInstance serviceInstance = balancerClient.choose("PRODUCT");
//        String format = String.format("http://%s:%s", serviceInstance.getHost(), serviceInstance.getPort()) + "msg";
//        RestTemplate restTemplate = new RestTemplate();
//        String forObject = restTemplate.getForObject(format, String.class);

//        第三种方式
//        String forObject = restTemplate.getForObject("http://PRODUCT/msg", String.class);
        List<String> orderIds = new ArrayList<>();
        orderIds.add("157875196366160022");
        orderIds.add("157875227953464068");
        List<ProductInfo> productInfos = productClient.listForOrder(orderIds);
        return ResultVOUtil.success(productInfos);
    }


    @GetMapping("/product/decreaseStock")
    public ResultVO decreaseStock() {
        return productClient.decreaseStock(Arrays.asList(new CartDTO("157875196366160022", 2)));
    }
}
